import 'package:flutter/material.dart';

/// 获取输入框中的文本
/// 1. 创建TextEditingController对象
/// 2. 为应用TextField应用TextEditingController对象
/// 3. 显示当前输入框的值

class RetrieveTextApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Retrieve Text Demo',
      theme: ThemeData(primarySwatch: Colors.red),
      home: RetrieveTextAppPage(),
    );
  }
}

class RetrieveTextAppPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => RetrieveTextAppPageState();
}

class RetrieveTextAppPageState extends State<RetrieveTextAppPage> {
  // 输入控制器
  final TextEditingController _controller = TextEditingController();

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Retrieve Text Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: TextField(
          controller: _controller,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          return showDialog(
              context: context,
              builder: (context) {
                return AlertDialog(
                  content: Text(_controller.text),
                );
              });
        },
        tooltip: 'Show me the value!',
        child: Icon(Icons.text_fields),
      ),
    );
  }
}
